home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / bank.zip / QUEUES.PAS < prev   
Pascal/Delphi Source File  |  1989-05-29  |  3KB  |  117 lines

  1. UNIT queues;
  2. (* Turbo Pascal 5.5 program *)
  3.  
  4. (**********************)
  5. (**)    INTERFACE   (**)
  6. (**********************)
  7. TYPE
  8.   GenericItem = OBJECT          {This object is nothing}
  9.     DESTRUCTOR done; virtual;   {by itself, but you can}
  10.   END;                          {make ANY other object }
  11.                                 {a descendant of it.   }
  12.  
  13.   ItemPtr = ^GenericItem;
  14.   ItemList = ^ItemNode;
  15.   ItemNode = RECORD
  16.     item : ItemPtr;
  17.     next : ItemList;
  18.   END;
  19.  
  20.   queue = OBJECT (GenericItem)
  21.     front, rear : ItemList;
  22.     length, maxlength : Word;
  23.     CONSTRUCTOR Init;
  24.     PROCEDURE MakeNull;
  25.     PROCEDURE enqueue(I : ItemPtr);
  26.     FUNCTION dequeue : ItemPtr;
  27.     FUNCTION empty : boolean;
  28.     FUNCTION GetLength : Word;
  29.     FUNCTION GetMax : Word;
  30.     DESTRUCTOR done; virtual;
  31.   END;
  32.  
  33. (**********************)
  34. (**) IMPLEMENTATION (**)
  35. (**********************)
  36.  
  37.   DESTRUCTOR GenericItem.done;   BEGIN END;
  38.  
  39.   CONSTRUCTOR queue.Init;
  40.   { To initialize a queue, create a "front" pointer
  41.     for it and set the "rear" pointer to the same
  42.     as the front.  Note that the actual queue of
  43.     items starts at "front^.next" -- "front" is
  44.     just a header node }
  45.   BEGIN
  46.     New(front);
  47.     front^.next := NIL;
  48.     Front^.item := NIL;
  49.     rear := front;
  50.     length := 0;
  51.     maxlength := 0;
  52.   END;
  53.  
  54.   PROCEDURE queue.MakeNull;
  55.   { To empty a queue, dispose of nodes until
  56.     the front of the line equals the rear }
  57.   VAR P : ItemList;
  58.   BEGIN
  59.     WHILE front <> rear DO
  60.       BEGIN
  61.         P := front;
  62.         front := front^.next;
  63.         IF P^.item <> NIL THEN
  64.           dispose(P^.item, done);
  65.         dispose(P);
  66.       END;
  67.   END;
  68.  
  69.   PROCEDURE queue.enqueue(I : ItemPtr);
  70.   { To enqueue an item, add it to the rear of the queue }
  71.   BEGIN
  72.     Inc(length);
  73.     IF length > maxlength THEN maxlength := length;
  74.     New(rear^.next);
  75.     rear := rear^.next;
  76.     rear^.item := I;
  77.     rear^.next := NIL;
  78.   END;
  79.  
  80.   FUNCTION queue.dequeue : ItemPtr;
  81.   { To dequeue an item, return the contents of the
  82.     frontmost node (the one after the "front" header),
  83.     advance the front pointer, and dispose of the
  84.     previous front }
  85.   VAR P : ItemList;
  86.   BEGIN
  87.     IF empty THEN dequeue := NIL
  88.     ELSE
  89.       BEGIN
  90.         dequeue := front^.next^.item;
  91.         P := front;
  92.         front := front^.next;
  93.         front^.item := NIL;
  94.         dispose(P);
  95.         Dec(length);
  96.       END;
  97.   END;
  98.  
  99.   FUNCTION queue.empty : boolean;
  100.   BEGIN  empty := front = rear;  END;
  101.  
  102.   FUNCTION queue.GetLength : Word;
  103.   BEGIN  GetLength := length;  END;
  104.  
  105.   FUNCTION queue.Getmax : Word;
  106.   BEGIN  Getmax := maxlength;  END;
  107.  
  108.   DESTRUCTOR queue.done;
  109.   { This procedure calls MakeNull to dispose
  110.     all of the nodes except "front", then
  111.     gets rid of "front" as well }
  112.   BEGIN
  113.     MakeNull;
  114.     dispose(front);
  115.   END;
  116.  
  117. END.